<?php
namespace app\blog\controller;
use cmf\controller\AdminBaseController;
use think\Db;

class FormController extends AdminBaseController
{
    //销售明细报表
    //单品报表
    public function orderDetail(){              
        $map=array();
        //查询条件
        $param = $this->request->param(); 
        $uid=cmf_get_current_admin_id();
        $uinfo=get_operator_data($uid);//登陆者信息
       if(isset($param['start_time'])&&!empty($param['start_time'])){
            $map['ctime'][]=array('egt',strtotime($param['start_time']));
        }
        if(isset($param['end_time'])&&!empty($param['end_time'])){
            $map['ctime'][]=array('egt',strtotime($param['end_time']));
        }
        $storeid=isset($param['storeid'])?$param['storeid']:'';
        if(isset($storeid) && !empty($storeid)){
            $map['storeid'] = $storeid;
            $where['id']=$storeid;
        }else{
            $userList=get_user_list($uid);
            $uid_list_str=toStr($userList,'userid');
            $uid_list_str.=','.$uid;
            $store_id_list=get_store_list($uid_list_str);
            if($store_id_list){
                $map['storeid']=array('in',$store_id_list);
                $where['id']=array('in',$store_id_list);
            }
        }
        
        $orderid=isset($param['orderid'])?$param['orderid']:'';
        if($orderid){
            $map['orderid'] = ['like', "%$orderid%"];
        }
        $goodsname=isset($param['goodsname'])?$param['goodsname']:'';
        if ($goodsname){
             $con['g_goodsname']=['like',"%$goodsname%"];
             $g_list=Db::name('Goods')->where($con)->field('g_id')->select()->toarray();
             $g_id_str=toStr($g_list,'g_id');            
             $map['goodsid']=array('in',$g_id_str);
        }
        $rebate=isset($param['rebate'])?$param['rebate']:'';        
        if(!empty($rebate)){
            $rebateid_str=rebateid($rebate);
            $map['rebateid']=array('in',$rebateid_str);
            $map['stemeal']=0;
        }
        $setmealid=isset($param['setmealid'])?$param['setmealid']:'';
        if($setmealid){
            $setid_str=setid($setmealid);
            $map['goodsid']=array('in',$setid_str);
            $map['stemeal']=1;    
        } 
        $paystatus=isset($param['paystatus'])?$param['paystatus']:'-3';       
        if($paystatus == 0){
            $map['paystatus']=$paystatus;
        }else{
            if(!empty($paystatus)){
                if($paystatus!=-3){
                    $map['paystatus']=$paystatus;
                }
            }
        }
        $list=Db::name('OrderDetail')->where($map)->paginate(10);
        $page = $list->render();
        //商品活动分类
        $rebate=Db::name('Rebate')->where(array('re_shopid'=>$storeid))->select()->toarray();
        $default=array('re_id'=>0,'re_name'=>'单品');        
        $rebate[]=$default;
        //付款状态
        $pays=get_pay_status(2);
        //套餐类型
        $setmeal_list=Db::name('Setmeal')->where(array('set_shopid'=>$storeid))->select()->toarray();
        $where['status']=1;
        $list_store_list=Db::name('Store')->where($where)->select()->toarray();//正在运营的店铺列表         
        $this->assign("list_store_list", $list_store_list);
        $this->assign("setmeal", $setmeal_list);
        $this->assign("pays", $pays);
        $this->assign("page", $page);
        $this->assign("list", $list);
        $this->assign("rebate", $rebate);
        return $this->fetch();
    }
    //套餐报表
    public function setMealDetail(){
        $storeid=50;//缓存里取
        $uid=1;//缓存里取
        $map=array();
        $param = $this->request->param();
        $orderid=isset($param['orderid'])?$param['orderid']:'';
        if($orderid) {
            $map['orderid'] = ['like', "%$orderid%"];
        }
        $paystatus=isset($param['paystatus'])?$param['paystatus']:'-3';        
        if($paystatus == 0){
            $map['paystatus']=$paystatus;
        }else{
            if(!empty($paystatus)){
                if($paystatus!=-3){
                    $map['paystatus']=$paystatus;
                }
            }
        }
        $setmeal_id=isset($param['setmeal'])?$param['setmeal']:'';
        if($setmeal_id) {
            $map['slog_setmealid'] = $setmeal_id;
        }
        $starttime=isset($param['start_time'])?$param['start_time']:'';
        $endtime=isset($param['end_time'])?$param['end_time']:'';
        if(!empty($starttime) ) {
            $map['slog_time'][]=array('egt',strtotime($starttime));
        }
        if(!empty($endtime) ) {
            $map['slog_time'][]=array('lt',24*60*60+strtotime($endtime));
        }
        $list=Db::name('Setmealog')->where($map)->paginate(10);
        //echo Db::name('Setmealog')->getlastsql(); 
        $page = $list->render();
        //获取套餐列表
        $setmeal=Db::name('Setmeal')->where(array('set_shopid'=>$storeid))->select();
        //付款状态
        $pays=get_pay_status(2);
        $this->assign("pays", $pays);
        $this->assign("page", $page);
        $this->assign("list", $list);
        $this->assign("setmeal", $setmeal);
        return $this->fetch();
    }
    
    //收银员明细（把这些明细做成tab显示）
    public function operator(){
        //这个是不是只有店长才可以看
        $storeid=50;
        $uid=1;//判断此人是不是店长权限
        $map=array();
        $param = $this->request->param();
       
        $orderNo=isset($param['orderNo'])?$param['orderNo']:'';
        if($orderNo) {
            $map['orderNo'] = ['like', "%$orderNo%"];
        } 
        $paystatus=isset($param['paystatus'])?$param['paystatus']:'-3';
        if($paystatus == 0){
            $map['paystatus']=$paystatus;
        }else{
            if(!empty($paystatus)){
                if($paystatus!=-3){
                    $map['paystatus']=$paystatus;
                }
            }
        }
        if($param['operatorid']){
            $map['operatorid']=$param['operatorid'];
        }
        $list=Db::name('Order')->where($map)->paginate(10);
        $page = $list->render();
        //付款状态
        $pays=get_pay_status(2);       
        $this->assign("operator_list", $operator_list);
        $this->assign("pays", $pays);
        $this->assign("list", $list);
        $this->assign("page", $page);
        return $this->fetch();
    }
    
    //营业额销售汇总
    //单品
    public function singleSummary(){
    
    }
    //套餐
    public function setMealSummary(){
    
    } 
      
}